﻿using ConfExpert.Forms.Aksesuar.Satinalma;
using ConfExpert.Forms.Base;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraReports.UI;
using DevTeam;
using System;
using System.Drawing;
using System.Windows.Forms;

namespace ConfExpert.Forms.Depo
{
    public partial class AksesuarRezerve : XForm
    {
        public AksesuarRezerve()
        {
            InitializeComponent();
        }

        int BrandId, SezonId;
        public AksesuarRezerve(string kaufIdxs, int brandId = 0, int sezonId = 0)
            : this()
        {
            KaufIdxs = kaufIdxs;
            BrandId = brandId;
            SezonId = sezonId;
        }

        BindingSource bsAksesuarIhtiyac;
        string KaufIdxs;
        private void AksesuarIhtiyaci_Load(object sender, EventArgs e)
        {
            
            ds.Merge(Session.DataManager.ExecuteDataSet("createAksesuarTalepforOrders", KaufIdxs, BrandId));

            ds.Tables["Table"].Columns.Add("KullanilabilirMiktar2", typeof(decimal), "IIF(IsNull(KullanilabilirMiktar, 0)<0,0,IsNull(KullanilabilirMiktar, 0))");
            ds.Tables["Table"].Columns.Add("RezerveMiktari", typeof(int), "IIF( IsNull(KullanilabilirMiktar2, 0) < IsNull(TalepEdilen, 0), IsNull(KullanilabilirMiktar2, 0), IsNull(TalepEdilen, 0))");
            ds.Tables["Table"].Columns.Add("EksikMiktar", typeof(decimal), "TalepEdilen-RezerveMiktari");


            bsAksesuarIhtiyac = new BindingSource(ds, "Table");
            InitGridView(gvTreeLines, bsAksesuarIhtiyac);

            gvTreeLines.MakeColumnInvisible("KullanilabilirMiktar2", "AksesuarRezerveLinesId", "Boyama", "AksSeriId", "RezerveEdilen");
            gvTreeLines.AddSummary("TalepEdilen", "KullanilabilirMiktar", "RezerveMiktari", "EksikMiktar");
            gvTreeLines.FormatAsNumber(0, "TalepEdilen", "KullanilabilirMiktar", "RezerveMiktari", "EksikMiktar");

            gvTreeLines.Columns["EksikMiktar"].SortOrder = DevExpress.Data.ColumnSortOrder.Descending;

            gvTreeLines.BestFitColumns();

            gvTreeLines.OptionsView.EnableAppearanceOddRow =
            gvTreeLines.OptionsView.EnableAppearanceEvenRow = false;

            gvTreeLines.RowCellStyle += new RowCellStyleEventHandler(gvTreeLines_RowCellStyle);
            btnEkle.ItemClick += btnEkle_ItemClick;
            btnRezerve.ItemClick += btnRezerve_ItemClick;
            //gvTreeLines.OptionsView.NewItemRowPosition = NewItemRowPosition.Bottom;
        }

        void btnRezerve_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            string xml = ds.Tables["Table"].Serialize();
            using (AksesuarRezerveOlustur ar = new AksesuarRezerveOlustur(xml, KaufIdxs, BrandId, SezonId))
                if (ar.ShowDialog() == DialogResult.OK)
                {
                    this.DialogResult = DialogResult.OK;
                    using (Talep ast = new Talep(ds.Tables["Table"], ar.BrandId, ar.SezonId, KaufIdxs))
                        if (ast.ShowDialog() == DialogResult.OK)
                        {
                            string AksPOId = ast.POIds.ToString();
                            Rapor.Aksesuar.xrAksesuarSiparisFoy asf = new Rapor.Aksesuar.xrAksesuarSiparisFoy(AksPOId, "GetAksesuarSiparisFoyu", "AksPOId");
                            asf.ShowPreview();
                        }
                }
        }

        void btnEkle_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gvTreeLines.EndEditing();
            string xml = ds.Tables["Table"].Serialize();
            using (AksesuarRezerveSec ar = new AksesuarRezerveSec())
                if (ar.ShowDialog() == DialogResult.OK)
                {
                    ExecuteNonQuery("AddAksesuartoRezerve", xml, KaufIdxs);
                    this.DialogResult = DialogResult.OK;
                    using (Talep ast = new Talep(ds.Tables["Table"], ar.BrandId, ar.SezonId, KaufIdxs))
                        if (ast.ShowDialog() == DialogResult.OK)
                        {
                            string AksPOId = ast.POIds.ToString();
                            Rapor.Aksesuar.xrAksesuarSiparisFoy asf = new Rapor.Aksesuar.xrAksesuarSiparisFoy(AksPOId, "GetAksesuarSiparisFoyu", "AksPOId");
                            asf.ShowPreview();
                        }
                }
        }

        void gvTreeLines_RowCellStyle(object sender, RowCellStyleEventArgs e)
        {
            GridView view = sender as GridView;

            int TalepEdilen = view.GetRowCellValue(e.RowHandle, "TalepEdilen").ToInt(-1);
            int KullanilabilirMiktar = view.GetRowCellValue(e.RowHandle, "KullanilabilirMiktar").ToInt(-1);
            //int AvailableQty = view.GetRowCellValue(e.RowHandle, "AvailableQty").ToInt(-1);
            if (TalepEdilen > KullanilabilirMiktar)
            {
                e.Appearance.BackColor = Color.LightPink;
            }
        }
    }
}